package work.chenbo.spring_boot_shiro.modules.sys_user.entity;


import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.shiro.crypto.hash.SimpleHash;
import work.chenbo.spring_boot_shiro.base.BaseEntity;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = true)
@Data
@TableName("sys_user")
public class SysUser extends BaseEntity {

    /** 用户名 */
    private String userName;

    /** 用户密码 */
    private String password;

    /** 真实姓名 */
    private String realName;

    /** 手机号 */
    private String phone;

    /** 邮箱 */
    @TableField(insertStrategy = FieldStrategy.IGNORED,
                updateStrategy = FieldStrategy.IGNORED,
                whereStrategy = FieldStrategy.DEFAULT
    )
    private String email;

    /** 用户昵称 */
    private String nickName;

    /** 用户权限 */
    private transient List<SysPermission> permissions;

    /** 用户角色 */
    private transient List<SysRole> roles;

    /**
     * 获取用户所有权限Sting编码 (Shiro用)
     * @author ChenBo
     * @date 2019/10/28
     */
    public Set<String> getStringPermissions(){
       Set<String> stringPermissions = new HashSet<>();
        if(permissions != null){
            stringPermissions = permissions.parallelStream()
                    .map(SysPermission::getCode)
                    .collect(Collectors.toSet());
        }
        return stringPermissions;

    }
    
    /**
     * 获取用户所有角色Sting编码 (Shiro用)
     * @author ChenBo
     * @date 2019/10/28
     */
    public Set<String> getStringRoles(){
        Set<String> stringRoles = new HashSet<>();
        if(roles != null){
            stringRoles = roles.parallelStream()
                    .map(SysRole::getCode)
                    .collect(Collectors.toSet());
        }
        return stringRoles;
    }


    /**
     * 获取加盐后的Md5密码
     * @author ChenBo
     * @date 2019/10/29
     */
    public String getMD5Password(String password,String salt){
        SimpleHash hash = new SimpleHash("MD5",password,userName,1024);
        return hash.toString();
    }


}